jun.BarangWin = Ext.extend(Ext.Window, {
    title: 'Master Barang',
    modez: 1,
    width: 400,
    height: 465,
    layout: 'form',
    modal: true,
    padding: 5,
    closeForm: false,
    initComponent: function() {
        this.items = [{
                xtype: 'form',
                frame: false,
                bodyStyle: 'background-color: #E4E4E4; padding: 10px',
                id: 'form-Barang',
                labelWidth: 100,
                labelAlign: 'left',
                layout: 'form',
                ref: 'formz',
                border: false,
                items: [{
                        xtype: 'uctextfield',
                        fieldLabel: 'Barcode',
                        hideLabel: false,
                        //hidden:true,
                        name: 'barcode',
                        id: 'barcodeid',
                        ref: '../barcode',
                        maxLength: 13,
                        //allowBlank: 1,
                        anchor: '100%'
                    }, {
                        xtype: 'uctextfield',
                        fieldLabel: 'Nama Barang',
                        hideLabel: false,
                        //hidden:true,
                        name: 'barang_name',
                        id: 'barang_nameid',
                        ref: '../barang_name',
                        maxLength: 100,
                        //allowBlank: 1,
                        anchor: '100%'
                    },
                    {
                        xtype: 'combo',
                        typeAhead: true,
                        triggerAction: 'all',
                        lazyRender: true,
                        mode: 'local',
                        fieldLabel: 'Kode Suplier',
                        store: jun.rztSupplier,
                        hiddenName: 'supplier_id',
                        valueField: 'supplier_id',
                        forceSelection: true,
                        displayField: 'supplier_code',
                        ref: '../supplier_code',
                        anchor: '100%'
                    },
                    {
                        xtype: 'checkbox',
                        fieldLabel: 'Bonus',
                        ideLabel: false,
                        value: 1,
                        inputValue: 1,
                        name: 'bonus',
                        ref: '../bonus'
                    },
                    {
                        xtype: 'checkbox',
                        fieldLabel: 'PPN',
                        hideLabel: false,
                        value: 1,
                        inputValue: 1,
                        uncheckedValue: 0,
                        name: 'ppn',
                        ref: '../ppn'
                    },
                    {
                        xtype: 'numericfield',
                        fieldLabel: 'Harga PCS',
                        hideLabel: false,
                        //hidden:true,
                        name: 'price_pcs',
                        id: 'price_pcsid',
                        ref: '../price_pcs',
                        maxLength: 30,
                        value:0,
                        allowBlank: false,
                        anchor: '100%'
                    }, {
                        xtype: 'numericfield',
                        fieldLabel: 'Harga LSN',
                        hideLabel: false,
                        allowBlank: false,
                        name: 'price_lsn',
                        id: 'price_lsnid',
                        ref: '../price_lsn',
                        maxLength: 30,
                        value:0,
                        anchor: '100%'
                    },
                    {
                        xtype: 'numericfield',
                        fieldLabel: 'PAK ke PCS',
                        hideLabel: false,
                        allowBlank: false,
                        name: 'pak_2_pcs',
                        id: 'pak_2_pcsid',
                        ref: '../pak_2_pcs',
                        maxLength: 30,
                        value:0,
                        anchor: '100%'
                    },
                    {
                        xtype: 'numericfield',
                        fieldLabel: 'Harga PAK',
                        hideLabel: false,
                        allowBlank: false,
                        name: 'price_pak',
                        id: 'price_pakid',
                        ref: '../price_pak',
                        maxLength: 30,
                        value:0,
                        anchor: '100%'
                    },
                    new jun.cmbSatuanMax({fieldLabel: "Satuan Maksimal", hideLabel: !1, width: 150, height: 20, name: "sat_max", id: "sat_maxid", ref: "../sat_max", hiddenName: "sat_max"}),
                    {
                        xtype: 'numericfield',
                        fieldLabel: 'Maks ke PCS',
                        hideLabel: false,
                        allowBlank: false,
                        name: 'max_2_pcs',
                        id: 'max_2_pcsid',
                        ref: '../max_2_pcs',
                        maxLength: 30,
                        value:0,
                        anchor: '100%'
                    },
                    {
                        xtype: 'numericfield',
                        fieldLabel: 'Harga Maksimal',
                        hideLabel: false,
                        allowBlank: false,
                        name: 'price_max',
                        id: 'price_maxid',
                        ref: '../price_max',
                        maxLength: 30,
                        value:0,
                        anchor: '100%'
                    },
                    {
                        xtype: 'numericfield',
                        fieldLabel: 'Harga Cost',
                        hideLabel: false,
                        allowBlank: false,
                        name: 'price_buy',
                        id: 'price_buyid',
                        ref: '../price_buy',
                        maxLength: 30,
                        value:0,
                        anchor: '100%'
                    },
                    {
                        xtype: 'numericfield',
                        fieldLabel: 'Stok Minimal',
                        hideLabel: false,
                        allowBlank: false,
                        name: 'stok_minimal',
                        id: 'stok_minimalid',
                        ref: '../stok_minimal',
                        maxLength: 30,
                        value:0,
                        anchor: '100%'
                    },
                    new jun.comboActive({fieldLabel: "Status", hideLabel: !1, width: 100, height: 20, name: "status", id: "statusid", ref: "../status", hiddenName: "status"})
                ]
            }];
        this.fbar = {
            xtype: 'toolbar',
            items: [{
                    xtype: 'button',
                    text: 'Simpan',
                    hidden: false,
                    ref: '../btnSave'
                }, {
                    xtype: 'button',
                    text: 'Simpan & Tutup',
                    ref: '../btnSaveClose'
                }, {
                    xtype: 'button',
                    text: 'Batal',
                    ref: '../btnCancel'
                }]
        };

        jun.BarangWin.superclass.initComponent.call(this);
        this.btnSaveClose.on('click', this.onbtnSaveCloseClick, this);
        this.btnSave.on('click', this.onbtnSaveclick, this);
        this.btnCancel.on('click', this.onbtnCancelclick, this);
        this.sat_max.on('blur', this.autoSetMaks, this);
        this.supplier_code.on('expand', this.onFocusSupplier, this);
        this.on("close", this.onWinClose, this);
        if (this.modez == 1 || this.modez == 2) {
            this.btnSave.setVisible(false);
        } else {
            this.btnSave.setVisible(true);
        }

    },
    onFocusSupplier: function() {
        jun.rztSupplier.FilterData();
    },
    onWinClose: function() {
        jun.rztSupplier.clearFilter();
    },
    btnDisabled: function(status) {
        this.btnSave.setDisabled(status);
        this.btnSaveClose.setDisabled(status);
    },
    autoSetMaks: function() {
        if (this.sat_max.getValue() === "KRAT")
        {
            this.max_2_pcs.setValue(24);
            this.max_2_pcs.setReadOnly(true);
        }
        else
        {
            this.max_2_pcs.setReadOnly(false);
        }
    },
    saveForm: function() {
        this.btnDisabled(true);
        var urlz;
        if (this.modez == 1 || this.modez == 2) {

            urlz = 'Barang/update/id/' + this.id;

        } else {

            urlz = 'Barang/create/';
        }

        Ext.getCmp('form-Barang').getForm().submit({
            url: urlz,
            timeOut: 1000,
            scope: this,
            success: function(f, a) {
                jun.rztBarang.reload();
                jun.rztBarangMaster.reload();
                jun.rztBarangLib.reload();
                var response = Ext.decode(a.response.responseText);
                Ext.MessageBox.show({
                    title: 'Info',
                    msg: response.msg,
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                });
                if (this.modez == 0) {
                    Ext.getCmp('form-Barang').getForm().reset();
                    this.btnDisabled(false);
                }
                if (this.closeForm) {
                    this.close();
                }
            },
            failure: function (f, a) {
                switch (a.failureType) {
                    case Ext.form.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.Action.CONNECT_FAILURE:
                        Ext.Msg.alert(a.response.statusText, a.response.responseText);
                        break;
                    case Ext.form.Action.SERVER_INVALID:
                        Ext.Msg.alert('Failure', a.result.msg);
                }
                this.btnDisabled(false);
            }

        });

    },
    onbtnSaveCloseClick: function() {
        this.closeForm = true;
        this.saveForm(true);
    },
    onbtnSaveclick: function() {
        this.closeForm = false;
        this.saveForm(false);
    },
    onbtnCancelclick: function() {
        this.close();
    }

});